using Core.RabbitMQ.Extensions;

namespace Core.WebApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            var configuration = builder.Services.BuildServiceProvider().GetRequiredService<IConfiguration>();

            builder.Services.AddRabbitMqService(options =>
            {
                var hostName = configuration["RabbitMq:Host"] ?? throw new Exception("HostName is not configured");
                var port = int.Parse(configuration["RabbitMq:Port"] ?? throw new Exception("Port is not configured"));
                var userName = configuration["RabbitMq:Username"] ?? throw new Exception("Username is not configured");
                var password = configuration["RabbitMq:Password"] ?? throw new Exception("Password is not configured");
                var virtualHost = configuration["RabbitMq:VirtualHost"] ?? "";
                var queueTtl = configuration["RabbitMq:QueueTtl"] ?? "";
                options.HostName = hostName;
                options.Port = port;
                options.UserName = userName;
                options.Password = password;
                options.VirtualHost = virtualHost;
                options.QueueTtl = int.Parse(queueTtl);
            });
            
            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseHttpsRedirection();

            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}
